<template>
  <div class="section">
    <el-form :model="form" :rules="rules" ref="ruleForm" size="medium" label-width="80px" class="notice_form">
      <el-form-item label="人员" prop="userName">
        <el-input placeholder="请输入" v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="telephone">
        <el-input placeholder="请输入" v-model="form.telephone" maxlength="11"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="userOrgName">
        <el-input placeholder="请输入" v-model="form.userOrgName"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class="btnBorder" size="medium" plain @click="handleCancel">取消</el-button>
      <el-button class="btnBg" size="medium" type="primary" @click="setData('ruleForm')">确定</el-button>
    </div>
  </div>
</template>

<script>
import { saveOrEditAssessmentPerson } from '@apis/mzEvaluation.js'
import { phoneReg } from '@utility/regExps.js'

export default {
  name: 'PeopleAddEdit',
  props: {
    item: {
      type: Object,
    },
  },
  data() {
    const checkPhone = (rule, value, cb) => {
      if (phoneReg.test(value)) {
        return cb()
      }
      cb(new Error('您输入的手机号码不是有效格式'))
    }
    return {
      form: {
        id: '',
        telephone: '',
        userName: '',
      },
      rules: {
        userName: [{ required: true, message: '请输入人员名称', trigger: 'blur' }],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkPhone,
            trigger: 'blur',
          },
        ],
        userOrgName: [{ required: true, message: '请输入部门', trigger: 'blur' }],
      },
    }
  },
  computed: {},
  created() {},
  watch: {
    item: {
      handler(newVal, oldVal) {
        this.form = { ...newVal }
      },
      // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
      immediate: true,
      deep: true,
    },
  },
  mounted() {},
  methods: {
    setData(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitData()
        } else {
          return false
        }
      })
    },
    submitData() {
      saveOrEditAssessmentPerson({
        ...this.form,
      }).then(res => {
        if (res.status === 200) {
          if (res.message) {
            this.$message({
              message: `${res.message}`,
              type: 'warning',
              duration: 6000,
            })
          } else {
            this.$message({
              message: '提交成功',
              type: 'success',
            })
            this.$emit('handleSuccess', false)
          }
        }
      })
    },
    // 关闭
    handleCancel() {
      this.$emit('handleCancel', false)
    },
  },
}
</script>

<style lang="scss" scoped>
.section {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .notice_form {
    padding-right: 20px;
  }
  .el-form-item {
    margin-bottom: 22px;
  }
  .footer {
    text-align: right;
  }
}
</style>
